﻿using AutoMapper;
using Equipment.API.Application.Service.DetectionmethodService;
using Equipment.API.DTOs.RequestDto.DefectlocationListRequest;
using Equipment.API.DTOs.ResponseDto.DefectlocationResponse;
using Equipment.Domain.Detectionmethod;
using Equipment.Errorcode;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Serilog;

namespace Equipment.API.Controllers
{
    /// <summary>
    /// 缺陷等级控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class DefectgradeController : ControllerBase
    {
        public readonly DefectgradeService _defectgradeService;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="defectgradeService"></param>

        public DefectgradeController(DefectgradeService defectgradeService)
        {
            _defectgradeService = defectgradeService;
        }
        /// <summary>
        /// 获取缺陷等级下拉列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult> GetDefectgradeDropdown()
        {
            return await _defectgradeService.GetDefectgradeDropdown();
        }

        /// <summary>
        /// 缺陷等级信息添加成功
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult> AddDefectgrade(AddDefectgradeResponseDto param)
        {
            return await _defectgradeService.AddDefectgrade(param);
        }

        /// <summary>
        /// 获取缺陷等级列表
        /// </summary>
        /// <param name="dto"></param>
        /// <param name="pageNumber"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult> GetDefectgrades([FromQuery]GetDefectgradeRequestDto dto, int pageNumber = 1, int pageSize = 2)
        {
            try
            {
                Log.Information("开始查询缺陷等级列表：{@Request}", dto);

                return await _defectgradeService.GetDefectgrades(dto, pageNumber, pageSize);
            }
            catch (AutoMapperMappingException ex)
            {
                Log.Error(ex, "数据映射异常：{Message}", ex.Message);
                return ApiResult.Fail("数据转换错误");
            }
            catch (Exception ex)
            {
                Log.Error(ex, "查询缺陷等级列表异常");
                return ApiResult.Fail("系统错误，查询失败");
            }
        }
        /// <summary>
        /// 更新缺陷等级信息
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<ApiResult> UpdateDefectgrade(UpdateDefectgradeRequestDto param)
        {
            try
            {
                Log.Information("开始更新缺陷等级：{@RequestData}", param);

                return await _defectgradeService.UpdateDefectgrade(param);
            }
            catch (DbUpdateConcurrencyException ex)
            {
                Log.Error(ex, "并发更新冲突：{Defectgradeid}", param?.Defectgradeid);
                return ApiResult.Fail("数据已被其他用户修改，请刷新后重试");
            }
            catch (Exception ex)
            {
                Log.Error(ex, "更新缺陷等级异常：{Defectgradeid}", param?.Defectgradeid);
                return ApiResult.Fail("系统错误，更新操作失败");
            }
        }

        /// <summary>
        /// 删除缺陷等级信息（软删除）
        /// </summary>
        [HttpDelete]
        public async Task<ApiResult> DeleteDefectgrade(int Defectgradeid)
        {
            try
            {
                Log.Information("删除缺陷等级信息：{DefectGradeId}", Defectgradeid);
                return await _defectgradeService.DeleteDefectgrade(Defectgradeid);
            }
            catch (Exception ex)
            {
                Log.Error(ex, "删除缺陷等级信息异常：{DefectGradeId}", Defectgradeid);
                return ApiResult.Fail("系统错误，删除操作失败"); // 统一错误响应
            }
        }


    }
}
